package com.jt.service;

import com.jt.pojo.User;
import com.jt.util.ObjectMapperUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
    @Override
    public List<User> findUserList() {
        List<User> userList=new ArrayList<>();
        CloseableHttpClient httpClient= HttpClients.createDefault();
        String url="http://www.jt.com/user/findUserList";
        HttpGet httpGet=new HttpGet(url);
        HttpResponse httpResponse= null;
        try {
            httpResponse = httpClient.execute(httpGet);
            int status=httpResponse.getStatusLine().getStatusCode();
            if (status==200){
                String result= EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
                userList= ObjectMapperUtil.toObj(result, userList.getClass());
                System.out.println(result);
                return userList;
            }

        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return null;
    }
}
